package com.wanxi.controller;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.ProductType;
import com.wanxi.service.ProductService;
import com.wanxi.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/product")
@CrossOrigin(origins = "http://localhost:8081")
public class ProductController {
    @Autowired
    ProductService productService;

    @PutMapping("/add")
    public String addProduct(ProductVO productVO,
                             @RequestParam("productTypeID") String productTypeID){
        productVO.setProductType(ProductType.builder().id(productTypeID).build());
        ResultDTO resultDTO = productService.addProduct(productVO);
        return JSONObject.toJSONString(resultDTO);
    }

    @DeleteMapping("/delete")
    public String deleteProduct(ProductVO productVO){
        ResultDTO resultDTO = productService.deleteProduct(productVO);
        return JSONObject.toJSONString(resultDTO);
    }

    @GetMapping("/findAll")
    public String findAllProduct(){
        ResultDTO allProduct = productService.findAllProduct();
        return JSONObject.toJSONString(allProduct);
    }

    @PostMapping("/findById")
    public String findProductById(ProductVO productVO){
        ResultDTO productById = productService.findProductById(productVO);
        return JSONObject.toJSONString(productById);
    }

    @GetMapping("/findBySome")
    public String findProductBySome(ProductVO productVO){
        ResultDTO productBySome = productService.findProductBySome(productVO);
        return JSONObject.toJSONString(productBySome);
    }

    @PostMapping("/updateEnable")
    public String updateProductEnable(ProductVO productVO){
        ResultDTO resultDTO = productService.updateEnable(productVO);
        return JSONObject.toJSONString(resultDTO);
    }

    @PostMapping("/update")
    public String updateProduct(ProductVO productVO,
                                @RequestParam("productTypeID") String productTypeID){
        productVO.setProductType(ProductType.builder().id(productTypeID).build());
        ResultDTO resultDTO = productService.updateProduct(productVO);
        return JSONObject.toJSONString(resultDTO);
    }
}
